package com.hbnu.jiani.menu;

import com.hbnu.jiani.service.PackageService;
import com.hbnu.jiani.service.UserService;

import java.util.Scanner;

public class ConsoleMenu {
    private UserService userService = new UserService();
    private PackageService packageService = new PackageService();
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            System.out.println("\n=== 快递管理系统 ===");
            System.out.println("1. 用户注册");
            System.out.println("2. 用户登录");
            System.out.println("3. 管理员登录");
            System.out.println("0. 退出");
            System.out.print("请选择: ");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    userService.register();
                    break;
                case "2":
                    String result = userService.userLogin();
                    if (result != null && !"admin".equals(result)) {
                        userMenu(result);
                    }
                    break;
                case "3":
                    if (userService.adminLogin()) { // 只有返回 true 才进菜单
                        adminMenu();
                    }
                    break;
                case "0":
                    System.out.println("再见！");
                    return;
                default:
                    System.out.println("无效选项！");
            }
        }
    }

    private void userMenu(String username) {
        while (true) {
            System.out.println("\n=== 用户菜单 ===");
            System.out.println("1. 查看我的快递");
            System.out.println("0. 退出登录");
            System.out.print("请选择: ");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    packageService.listUserPackages(username);
                    break;
                case "0":
                    return;
                default:
                    System.out.println("无效选项！");
            }
        }
    }

    private void adminMenu() {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1. 添加快递");
            System.out.println("2. 删除快递");
            System.out.println("3. 修改快递");
            System.out.println("4. 查看所有快递");
            System.out.println("0. 退出登录");
            System.out.print("请选择: ");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1": packageService.addPackage(); break;
                case "2": packageService.deletePackage(); break;
                case "3": packageService.updatePackage(); break;
                case "4": packageService.listAllPackages(); break;
                case "0": return;
                default: System.out.println("无效选项！");
            }
        }
    }
}
